<?php

namespace helper;

use think\helper\Str;

class FormBuilder
{

    /**
     * 生成文本框(按类型)
     * @param $type
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _input($type, $name, $value, $options = [])
    {
        if (!isset($options['name'])) $options['name'] = $name; //补充name属性
        $options['class'] = isset($options['class']) //补充name属性
            ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control')
            : 'form-control';
        if (!in_array($type, ['checkbox', 'radio'])) {
            $options['value'] = $value;
        }
        $options['type'] = $type;
        return '<input' . self::attributes($options) . ' />';
    }

    /**
     * 生成普通文本框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _text($name, $value, $options = [])
    {
        return self::_input('text', $name, $value, $options);
    }

    /**
     * 生成密码文本框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _password($name, $value, $options = [])
    {

        return self::_input('password', $name, $value, $options);
    }

    /**
     * 生成隐藏文本框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _hidden($name, $value, $options = [])
    {
        return self::_input('hidden', $name, $value, $options);
    }

    /**
     * 日期选择器
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _laydate($name, $value, $options = [])
    {
        if (empty($value)) $value = '';
        if (is_numeric($value) && strlen($value) === 10) {
            $dateFormat = [
                'yyyy' => 'Y',
                'MM'   => 'm',
                'dd'   => 'd',
                'HH'   => 'H',
                'mm'   => 'i',
                'ss'   => 's',
            ];
            $format     = 'Y-m-d';
            if (isset($options['data-format'])) {
                $format = str_replace(array_keys($dateFormat), array_values($dateFormat), $options['data-format']);
            }
            $value = date($format, $value);
        }
        $options['class'] = isset($options['class']) ? 'form-control f-laydate ' . $options['class'] : 'form-control f-laydate';
        return self::_input('text', $name, $value, $options);
    }

    /**
     * 生成数字框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _number($name, $value, $options = [])
    {
        if (!isset($options['data-type'])) $options['data-type'] = 'float';
        $options['class'] = isset($options['class']) ? 'form-control f-number' . $options['class'] : 'form-control f-number';
        return self::_input('text', $name, $value, $options);
    }

    /**
     * 生成颜色选择框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _color($name, $value, $options = [])
    {
        $options['class'] = isset($options['class']) ? 'form-control f-color' . $options['class'] : 'form-control f-color';
        return self::_input('text', $name, $value, $options);
    }

    /**
     * 生成多行文本框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _textarea($name, $value, $options = [])
    {
        if (!isset($options['name'])) $options['name'] = $name;
        if (!isset($options['rows'])) $options['rows'] = 3;
        $value            = Utils::htmlOut($value);
        $options['class'] = isset($options['class']) ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control') : 'form-control';
        $options          = self::attributes($options);
        return '<div class="form-control-static"><textarea' . $options . '>' . $value . '</textarea></div>';
    }

    /**
     * switch开关
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _switch($name, $value, $options = [])
    {
        $themeCss = isset($options['theme-css']) ? $options['theme-css'] : 'switch-solid switch-primary';
        if (!isset($options['name'])) $options['name'] = $name;
        $options['value'] = $options['value'] ?? 1;
        $options          = self::attributes($options);
        $checked          = $value == '1' ? 'checked' : '';
        return <<<EOT
<div class="form-control-static">
<label class="switch {$themeCss}">
      <input type="checkbox" {$options} $checked />
      <span></span>
</label>
</div>
EOT;
    }

    /**
     * tags标签框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _tags($name, $value, $options = [])
    {
        $options['class'] = isset($options['class']) ? 'f-tags' . $options['class'] : 'f-tags';
        return self::_input('text', $name, $value, $options);
    }

    /**
     * 富文本编辑器
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _ckeditor($name, $value, $options = [])
    {
        $options['class'] = isset($options['class']) ? 'f-ck-editor ' . $options['class'] : 'f-ck-editor';
        $options['module'] = isset($options['module']) ? $options['module'] : 'system';
        $value            = Utils::htmlOut($value);
        return '<div class="form-control-static">' . self::_textarea($name, $value, $options) . '</div>';
    }

    /**
     * 生成代码编辑器
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _ace($name, $value, $options = [])
    {
        $options['id']        = $name;
        $options['class']     = isset($options['class']) ? ' f-ace' . $options['class'] : 'f-ace';
        $options['data-mode'] = isset($options['data-mode']) ? $options['data-mode'] : 'html';
        $options              = self::attributes($options);
        return <<<EOT
<textarea name="{$name}" class="hidden">{$value}</textarea>
<pre {$options}>{$value}</pre>
EOT;
    }

    /**
     * 省市区选择
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _area($name, $value, $options = [])
    {
        $options = self::attributes($options);
        return <<<EOT
<div class="f-area" data-name="{$name}" {$options}>
    <input type="hidden" name="{$name}" value="{$value}" />
    <select class="form-control prov"></select>
    <select class="form-control city"></select>
    <select class="form-control area"></select>
</div>
EOT;
    }


    /**
     * 生成下拉列表框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _select($name, $value, $options = [])
    {
        if (!isset($options['items-data'])) return '数据源不能为空！';
        if (!isset($options['items-key'])) $options['items-key'] = 'key';
        if (!isset($options['items-name'])) $options['items-name'] = 'name';
        if (!isset($options['name'])) $options['name'] = $name;
        $options['class'] = isset($options['class'])
            ? $options['class'] . (stripos($options['class'], 'form-control') !== false ? '' : ' form-control')
            : 'form-control';
        $html             = [];
        if (isset($options['empty-option'])) {
            $emptyOption = explode('|', $options['empty-option']);
            $html[]      = '<option value="' . $emptyOption[0] . '">' . $emptyOption[1] . '</option>';
        }
        $list     = self::getListData($options['items-data']);
        $multiple = isset($options['multiple']);
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                if ($multiple === true) {
                    $arr = $value;
                    if (is_string($value)) {
                        $arr = empty($value) ? [] : explode(',', $value);
                    }
                    $selectedHtml = (in_array($v[$options['items-key']], $arr)) ? 'selected' : '';
                } else {
                    $selectedHtml = ($v[$options['items-key']] == $value) ? 'selected' : '';
                }
                $disabledHtml = $v['disabled'] ?? '';
                if(Str::contains($options['items-name'],',')){
                    $itemsNameArr = explode(',',$options['items-name']);
                    $itemsName = [];
                    foreach($itemsNameArr as $name){
                        $itemsName[] = $v[$name];
                    }
                    $itemsName = implode('|',$itemsName);
                }else{
                    $itemsName = $v[$options['items-name']];
                }
                $html[]       = '<option value="' . $v[$options['items-key']] . '" ' . $selectedHtml . ' ' . $disabledHtml . '>' . $itemsName . '</option>';
            }
        }
        $options = self::attributes($options);
        $list    = implode(PHP_EOL, $html);
        return "<select {$options}>{$list}</select>";
    }

    /**
     * 生成复选框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _checkbox($name, $value, $options = [])
    {
        if (!isset($options['items-data'])) return '数据源不能为空！';
        if (!isset($options['items-key'])) $options['items-key'] = 'key';
        if (!isset($options['items-name'])) $options['items-name'] = 'name';
        $themeCss  = isset($options['theme-css']) ? $options['theme-css'] : 'checkbox-primary';
        $itemsKey  = $options['items-key']; //键
        $itemsName = $options['items-name']; //显示值
        $html      = [];
        $list      = self::getListData($options['items-data']);
        if (!empty($list)) {
            $options = self::attributes($options);
            foreach ($list as $k => $v) {
                $checked = in_array($v[$itemsKey], explode(',', $value)) ? 'checked' : '';
                $html[]  = <<<EOT
<label class="checkbox checkbox-inline {$themeCss}">
    <input type="checkbox" {$options} name="{$name}[]" value="{$v[$itemsKey]}" {$checked} /><span>{$v[$itemsName]}</span>
</label>
EOT;
            }
        }
        return implode(PHP_EOL, $html);
    }

    /**
     * 生成单选框
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _radio($name, $value, $options = [])
    {
        if (!isset($options['items-data'])) return '数据源不能为空！';
        if (!isset($options['items-key'])) $options['items-key'] = 'key';
        if (!isset($options['items-name'])) $options['items-name'] = 'name';
        $themeCss  = isset($options['theme-css']) ? $options['theme-css'] : 'radio-primary';
        $itemsKey  = $options['items-key']; //键
        $itemsName = $options['items-name']; //显示值
        $html      = [];
        $list      = self::getListData($options['items-data']);
        if (!empty($list)) {
            $options = self::attributes($options);
            foreach ($list as $k => $v) {
                $checked = $v[$itemsKey] == $value ? 'checked' : '';
                $html[]  = <<<EOT
<label class="radio radio-inline {$themeCss}">
    <input type="radio" {$options} name="{$name}" value="{$v[$itemsKey]}" {$checked} /><span>{$v[$itemsName]}</span>
</label>
EOT;
            }
        }
        return implode(PHP_EOL, $html);
    }

    /**
     * selectPage控件
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     */
    static public function _selectPage($name, $value, $options = [])
    {
        if (!isset($options['name'])) $options['name'] = $name; //补充name属性
        $options['class'] = isset($options['class']) ? $options['class'] : 'form-control f-selectPage';
        $options['value'] = $value;
        $options          = self::attributes($options);
        return <<<EOT
<input type="text" {$options} />
EOT;
    }

    /**
     * 项目列表
     * @param $name
     * @param $value
     * @param array $options data-files=[]
     * @return string
     * @throws \Exception
     */
    static public function _inputGroup($name, $value, $options = [])
    {
        $files = $options['data-files'];
        $value = !empty($value) ? json_decode($value, true) : [];
        $thead = '';
        $item  = '';
        foreach ($files as $k => $v) {
            $width = isset($v['width']) ? 'width:'.$v['width'] : '';
            $thead .= <<<EOT
        <th style="{$width}">{$v['name']}</th>
EOT;
        }
        $thead .= '<th style="width:160px">操作</th>';
        if (!empty($value)) {
            $rows = [];
            foreach ($value as $k => $v) {
                $row = '<tr class="item exist">';
                foreach ($files as $kk => $vv) {
                    $class = $vv['class'] ?? '';
                    $inputType = $vv['input_type'] ?? '';
                    $row .=self::inputType($inputType,$kk,$vv,$v[$kk],$class);
                }
                $row    .= <<<EOT
        <td>
            <a href="javascript:;" onclick="formHelper.inputGroupup(this)" class="btn btn-default btn-up"><i class="mdi mdi-arrow-up"></i> </a>
            <a href="javascript:;" onclick="formHelper.inputGroupdown(this)" class="btn btn-default btn-down"><i class="mdi mdi-arrow-down"></i> </a>
            <a href="javascript:;" onclick="formHelper.inputGroupDel(this)" class="btn btn-danger btn-del"><i class="mdi mdi-delete"></i> </a>
            <a href="javascript:;" onclick="formHelper.inputGroupAdd(this)" class="btn btn-info btn-add"><i class="mdi mdi-plus"></i> </a>
        </td>
</tr>
EOT;
                $rows[] = $row;
            }
            $item = implode(PHP_EOL, $rows);
        }else{
                $item = '<tr class="item exist">';
                foreach ($files as $kk => $vv) {
                    $class = $vv['class'] ?? '';
                    $inputType = $vv['input_type'] ?? '';
                    $item .=self::inputType($inputType,$kk,$vv,'',$class);
                }
                $item    .= <<<EOT
        <td>
            <a href="javascript:;" onclick="formHelper.inputGroupup(this)" class="btn btn-default btn-up"><i class="mdi mdi-arrow-up"></i> </a>
            <a href="javascript:;" onclick="formHelper.inputGroupdown(this)" class="btn btn-default btn-down"><i class="mdi mdi-arrow-down"></i> </a>
            <a href="javascript:;" onclick="formHelper.inputGroupDel(this)" class="btn btn-danger btn-del"><i class="mdi mdi-delete"></i> </a>
            <a href="javascript:;" onclick="formHelper.inputGroupAdd(this)" class="btn btn-info btn-add"><i class="mdi mdi-plus"></i> </a>
        </td>
</tr>
EOT;
        }
        $fun = '';
        if(isset($options['data-setname-after'])) $fun.='data-setname-after="'.$options['data-setname-after'].'"'.' ';
        if(isset($options['data-add-before'])) $fun.='data-add-before="'.$options['data-add-before'].'"'.' ';
        if(isset($options['data-add-after'])) $fun.='data-setname-after="'.$options['data-setname-after'].'"'.' ';
        if(isset($options['data-del-before'])) $fun.='data-del-before="'.$options['data-del-before'].'"'.' ';
        if(isset($options['data-del-after'])) $fun.='data-del-after="'.$options['data-del-after'].'"'.' ';
        return <<<EOT
<table class="table f-input-group" data-name="{$name}" {$fun}>
   <thead>
        <tr>
           {$thead}
        </tr>
    </thead>
    <tbody>
        {$item}
    </tbody>
</table>
EOT;
    }

    /**
     * 输入类型
     * @param $type
     * @param string $value
     */
    static public function inputType($type,$name,$options,$value=''){
        switch ($type) {
            case 'f-upload':
                $data = <<<EOT
        <td>
        <div class="input-group f-upload" data-type="image" data-theme="singleImage01">
            <input class="hide" type="file" accept="image/*" />
            <input type="text" class="form-control item-input file-url" data-name="$name" value="$value" />
            <span class="input-group-btn">
            <button class="btn btn-default" type="button"
                    data-type="single"
                    data-file="image"
                    onclick="themeCallFn.selectFile(this);">
            <i class="mdi mdi-select"></i>
            </button>
            </span>
            <span class="input-group-btn">
                <button class="btn btn-default" type="button" onclick="themeCallFn.singleImage01.preview(this)">
                <i class="mdi mdi-eye"></i> 
                </button>
            </span>
        </div>
        </td>
EOT;
                break;
            case 'select':
                $options['data-name'] = $name;
                $html = self::_select('',$value,$options);
                $data = <<<EOT
<td>{$html}</td>
EOT;
                break;
            default:
                $data = <<<EOT
        <td>
            <input type="text" class='form-control item-input {$type}' data-name="$name" value="$value" />
        </td>
EOT;
        }
        return $data;
    }

    /**
     * 图片图片上传
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     * @throws \Exception
     */
    static public function _uploadImage($name, $value, $options = [])
    {
        $theme   = isset($options['data-theme']) ? $options['data-theme'] : 'singleImage01';
        $dataType = '';
        if(isset($options['dataType'])){
            $dataType = 'dataType="'.$options['dataType'].'"';
            unset($options['dataType']);
        }
        $options = self::attributes($options);
        $h       = '';
        switch ($theme) {
            case 'singleImage01':
                $h = <<<EOT
<div class="input-group f-upload" {$options} data-type="image" data-theme="singleImage01" data-name='{$name}'>
    <input class="hide" type="file" accept="image/*" /> 
    <input type="text" class="form-control file-url" name="{$name}" value="{$value}" {$dataType}/>
    <span class="input-group-btn">
        <button class="btn btn-default upload-btn" type="button"><i class="mdi mdi-file-image"></i> 上传</button>
    </span>
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" 
        data-type="single" 
        data-file="image" 
        onclick="themeCallFn.selectFile(this);">
        <i class="mdi mdi-select"></i> 选择图片
        </button>
    </span>
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" onclick="themeCallFn.singleImage01.preview(this)"><i class="mdi mdi-eye"></i> 预览</button>
    </span>
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" onclick="themeCallFn.singleImage01.del(this)"><i class="mdi mdi-delete"></i> 删除</button>
    </span>
</div>
EOT;
                break;
        }
        return $h;
    }

    /**
     * 多图上传
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     * @throws \Exception
     */
    static public function _uploadImages($name, $value, $options = [])
    {
        $theme   = isset($options['data-theme']) ? $options['data-theme'] : 'multiImage01';
        $dataType = '';
        if(isset($options['dataType'])){
            $dataType = 'dataType="'.$options['dataType'].'"';
            unset($options['dataType']);
        }
        $options = self::attributes($options);
        $h       = '';
        switch ($theme) {
            case 'multiImage01':
                $itemHtml = [];
                if(!empty($value)){
                    $items = explode(',',$value); //value逗号隔开数据
                    foreach($items as $url){
                        $itemHtml[] = <<<ITEM
<li class="item-row" style="background:url({$url}) no-repeat center center / cover;">
    <div class="icon-bar">
        <input type="hidden" class="file-url" value="{$url}" {$dataType} />
        <i class="mdi mdi-arrow-left text-white" onclick="themeCallFn.multiImage01.upMove(this)"></i>
        <i class="mdi mdi-arrow-right text-white" onclick="themeCallFn.multiImage01.downMove(this)"></i>
        <i class="mdi mdi-delete text-white" onclick="themeCallFn.multiImage01.del(this)"></i>
        <i class="mdi mdi-eye text-white" onclick="themeCallFn.multiImage01.preview(this)"></i>
    </div>
</li>
ITEM;
                    }
                }
                $itemHtml = implode(PHP_EOL,$itemHtml);
                $h = <<<EOT
<div class="f-upload" {$options} data-type="image" data-theme="{$theme}" data-name="{$name}">
    <input class="hide" type="file" accept="image/*" multiple/>
    <input type="hidden" name="{$name}" value="{$value}" {$dataType} style="width:100%;" />
    <button type="button" class="btn btn-default upload-btn">
        <i class="mdi mdi-upload"></i> 上传图片
    </button>
    <button type="button" class="btn btn-default" data-type="multi" data-file="image" onclick="themeCallFn.selectFile(this);">
        <i class="mdi mdi-select"></i> 选择图片
    </button>
    <ul class="upload-list upload-multi01-list">
{$itemHtml}
    </ul>
</div>
<div class="clearfix"></div>
EOT;
                break;
            case 'multiImage02':
                $itemHtml = [];
                if(!empty($value)){
                    $items = json_decode($value,true); //json转义
                    foreach($items as $item){
                        $itemHtml[] = <<<ITEM
<li class="item-row">
            <div class="g-bd">
                <div class="g-sd">
                    <div class="image" style="background:url({$item['file_url']}) no-repeat center center / cover;">
                        <div class="icon-bar">
                            <i class="mdi mdi-arrow-left text-white" onclick="themeCallFn.multiImage02.upMove(this)"></i>
                            <i class="mdi mdi-arrow-right text-white" onclick="themeCallFn.multiImage02.downMove(this)"></i>
                            <i class="mdi mdi-delete text-white" onclick="themeCallFn.multiImage02.del(this)"></i>
                            <i class="mdi mdi-eye text-white" onclick="themeCallFn.multiImage02.preview(this)"></i>
                        </div>
                    </div>
                </div>
                <div class="g-mn">
                    <div class="g-mnc">
                        <input type="hidden" class="file-url" value="{$item['file_url']}" {$dataType}/>
                        <input type="text" class="form-control input-sm file-s1" onBlur="themeCallFn.multiImage02.blur(this);" value="{$item['s1']}" />
                        <textarea class="form-control input-sm file-s2" onBlur="themeCallFn.multiImage02.blur(this);" style="height:60px;">{$item['s2']}</textarea>
                    </div>
                </div>
            </div>
        </li>
ITEM;
                    }
                }
                $itemHtml = implode(PHP_EOL,$itemHtml);
                $h = <<<EOT
<div class="f-upload" {$options} data-type="image" data-theme="multiImage02" data-name="{$name}">
    <input class="hide" type="file" accept="image/*" multiple/>
    <textarea name="{$name}" style="display: none;">{$value}</textarea>
    <button type="button" class="btn btn-default upload-btn">
        <i class="mdi mdi-upload"></i> 上传图片
    </button>
    <button type="button" class="btn btn-default" data-type="multi" data-file="image" onclick="themeCallFn.selectFile(this);">
        <i class="mdi mdi-select"></i> 选择图片
    </button>
    <ul class="upload-list upload-multi02-list">
{$itemHtml}
    </ul>
</div>
<div class="clearfix"></div>
EOT;
                break;
        }
        return $h;
    }

    /**
     * 附件上传
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     * @throws \Exception
     */
    static public function _uploadFile($name, $value, $options = [])
    {
        $theme   = isset($options['data-theme']) ? $options['data-theme'] : 'singleFile01';
        $dataType = '';
        if(isset($options['dataType'])){
            $dataType = 'dataType="'.$options['dataType'].'"';
            unset($options['dataType']);
        }
        $options = self::attributes($options);
        $h       = '';
        switch ($theme) {
            case 'singleFile01':
                $h = <<<EOT
<div class="input-group f-upload" {$options} data-type="file" data-theme="singleFile01" data-name='{$name}'>
    <input class="hide" type="file" /> 
    <input type="text" class="form-control file-url" name="{$name}" value="{$value}" {$dataType}/>
    <span class="input-group-btn">
        <button class="btn btn-default upload-btn" type="button"><i class="mdi mdi-file-excel"></i> 上传</button>
    </span>
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" 
        data-type="single" 
        data-file="doc,xls,txt,ppt,pdf,image" 
        onclick="themeCallFn.selectFile(this);">
        <i class="mdi mdi-select"></i> 选择附件
        </button>
    </span>
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" onclick="themeCallFn.singleFile01.preview(this)"><i class="mdi mdi-eye"></i> 预览</button>
    </span>
    <span class="input-group-btn">
        <button class="btn btn-default" type="button" onclick="themeCallFn.singleFile01.del(this)"><i class="mdi mdi-delete"></i> 删除</button>
    </span>
</div>
EOT;
                break;
            case 'singleFile02':

                break;
        }
        return $h;
    }

    /**
     * 多附件上传
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     * @throws \Exception
     */
    static public function _uploadFiles($name, $value, $options = [])
    {
        $theme   = isset($options['data-theme']) ? $options['data-theme'] : 'multiFile01';
        $dataType = '';
        if(isset($options['dataType'])){
            $dataType = 'dataType="'.$options['dataType'].'"';
            unset($options['dataType']);
        }
        $options = self::attributes($options);
        $h       = '';
        switch ($theme) {
            case 'multiFile01':
                $itemHtml = [];
                if(!empty($value)){
                    $items = json_decode($value,true); //json转义
                    foreach($items as $item){
                        $itemHtml[] = <<<ITEM
        <li class="item-row">
            <input type="hidden" class="file-ext" value="{$item['file_ext']}" {$dataType}/>
            <div class="file-ext {$item['file_ext']}"></div>
            <div class="input-group m-b-10">
                <span class="input-group-addon">名称</span>
                <input type="text" class="form-control file-name" value="{$item['file_name']}" placeholder="文件名称" />
            </div>
            <div class="input-group">
                <span class="input-group-addon">路径</span>
                <input type="text" class="form-control file-url" value="{$item['file_url']}" readonly />
            </div>
            <div class="icon-bar">
                <i class="mdi mdi-arrow-up" onclick="themeCallFn.multiFile01.upMove(this)"></i>
                <i class="mdi mdi-arrow-down" onclick="themeCallFn.multiFile01.downMove(this)"></i>
                <i class="mdi mdi-delete" onclick="themeCallFn.multiFile01.del(this)"></i>
            </div>
        </li>
ITEM;
                    }
                }
                $itemHtml = implode(PHP_EOL,$itemHtml);
                $h = <<<EOT
<div class="f-upload" {$options} data-type="file" data-theme="multiImage02" data-name="{$name}">
    <input class="hide" type="file" multiple />
    <textarea name="{$name}" style="display: none;" {$dataType}>{$value}</textarea>
    <button type="button" class="btn btn-default upload-btn">
        <i class="mdi mdi-upload"></i> 上传文件
    </button>
     <button class="btn btn-default" type="button" 
        data-type="multi" 
        data-file="doc,xls,txt,ppt,pdf,image" 
        onclick="themeCallFn.selectFile(this);">
        <i class="mdi mdi-select"></i> 选择附件
     </button>
    <ul class="upload-list upload-multi03-list">
{$itemHtml}
    </ul>
</div>
<div class="clearfix"></div>
EOT;
                break;
        }
        return $h;
    }

    /**
     * 视频上传
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     * @throws \Exception
     */
    static public function _uploadVideo($name, $value, $options = [])
    {
        $vodType = config('cloud.vod.default');
        $options = self::attributes($options);
        $arr = ['cover_url'=>'','vid'=>''];
        if(!empty($value)) $arr = json_decode($value,true);
        $uploadListStyle = empty($value) ? "display:none;" : " ";
        $uploadVideoBtnStyle = empty($value) ? "" : "display:none;";
        $h       = <<<EOT
<div class="upload-video-box">
    <textarea type="hidden" name="{$name}" {$options} style="display: none;">{$value}</textarea>
    <div class="upload-list" style="{$uploadListStyle}">
        <div class="upload-video-container item-row">
            <div class="image" style="background-image:url({$arr['cover_url']})">
                <div class="icon-bar text-center">
                    <i class="mdi mdi-delete text-white" onclick="uploadVideo.delVod(this)"></i>
                    <i class="mdi mdi-play text-white" onclick="helper.doDialog(this);"
                       data-url="/system/vod.{$vodType}.video/player.html?video_id={$arr['vid']}"
                       data-title="视频播放"
                       data-width="620px"
                       data-height="440px"></i>
                </div>
            </div>
        </div>
    </div>
    <a href="javascript:;" class="upload-video-container upload-video-btn" style="{$uploadVideoBtnStyle}"
       data-title="选择视频"
       data-url="/system/vod.{$vodType}.video/select.html?type=single&field={$name}"
       data-width="800px"
       data-height="500px"
       onclick="helper.doDialog(this)"
    >
        <div class="text-center">
            <img src="/static/system/images/record.png" style="width: 35px">
            <div class="text-dark">上传视频</div>
        </div>
    </a>
</div>
EOT;
        return $h;
    }

    /**
     * 多个视频上传
     * @param $name
     * @param $value
     * @param array $options
     * @return string
     * @throws \Exception
     */
    static public function _uploadVideos($name, $value, $options = []){
        $vodType = config('cloud.vod.default');
        $options = self::attributes($options);
        $itemHtml = [];
        if(!empty($value)){
            $arr = json_decode($value,true);
            foreach($arr as $v){
                $json = json_encode($v,JSON_UNESCAPED_UNICODE);
                $itemHtml[] = <<<EOT
    <div class="upload-video-container item-row" data-val='{$json}'>
        <div class="image" style="background-image: url({$v['cover_url']})">
            <div class="icon-bar">
                <i class="mdi mdi-arrow-left text-white" onclick="uploadVideo.upMove(this)"></i>
                <i class="mdi mdi-arrow-right text-white" onclick="uploadVideo.downMove(this)"></i>
                <i class="mdi mdi-delete text-white" onclick="uploadVideo.delRow(this)"></i>
                <i class="mdi mdi-play text-white" onclick="helper.doDialog(this);"
                   data-url="/system/vod.${vodType}.video/player.html?video_id={$v['vid']}"
                   data-title="视频播放"
                   data-width="620px"
                   data-height="440px"></i>
            </div>
        </div>
    </div>
EOT;
            }
        }
        $itemHtml = implode(PHP_EOL,$itemHtml);
        $h = <<<EOT
<div class="upload-video-box form-control-static">
    <textarea type="hidden" name="{$name}" {$options} style="display: none;">{$value}</textarea><br/>
    <div class="upload-list">
    {$itemHtml}
    </div>
    <a href="javascript:;" class="upload-video-container upload-video-btn"
       data-title="选择视频"
       data-url="/system/vod.{$vodType}.video/select.html?type=multi&field={$name}"
       data-width="800px"
       data-height="500px"
       onclick="helper.doDialog(this)"
    >
        <div class="text-center">
            <img src="/static/system/images/record.png" style="width: 35px">
            <div class="text-dark">上传视频</div>
        </div>
    </a>
</div>
EOT;
        return $h;
    }

    /*
     * 数组转换字符串
     * @param  array $options
     * @return string
     */
    static protected function attributes($options)
    {
        $html = [];
        foreach ((array)$options as $key => $value) {
            if (is_numeric($key)) $key = $value;
            $element = self::attributeElement($key, $value);
            if (!is_null($element)) $html[] = $element;
        }
        return count($html) > 0 ? ' ' . implode(' ', $html) : '';
    }

    /**
     * 拼接属性
     * @param $key
     * @param $value
     * @return string|null
     */
    static protected function attributeElement($key, $value)
    {
        if (in_array($key, ['theme-css', 'items-data', 'items-key', 'items-name', 'empty-option'])) return null;
        if (!is_null($value)) {
            if (is_array($value) || stripos($value, '"') !== false) {
                $value = is_array($value) ? json_encode($value, JSON_UNESCAPED_UNICODE) : $value;
                return $key . "='" . $value . "'";
            } else {
                return $key . '="' . $value . '"';
            }
        }
        return '';
    }

    /**
     * 解析传入数据
     * @param $listData
     * @return array|mixed
     */
    static public function getListData($listData)
    {
        if (is_string($listData)) {
            $listData = explode(":", (string)$listData);
            $list     = [];
            switch ($listData[0]) {
                case 'url': //调用url回调 url:数据源地址
                    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
                    $data     = http::get($protocol . $_SERVER['HTTP_HOST'] . $listData[1]);
                    $list     = json_decode($data, true);
                    $list     = is_array($list) ? $list : [];
                    break;
                case 'fun': //执行指定方法 fun:方法名:参数1,参数2,参数3
                    $fun    = $listData[1];
                    $params = isset($listData[2]) ? explode(',', $listData[2]) : [];
                    $list   = call_user_func_array($fun, $params);
                    break;
                case 'class': //执行指定类方法 类路径需\\隔开 class:calss类名:方法名:参数1,参数2
                    $class  = $listData[1];
                    $fun    = $listData[2];
                    $params = isset($listData[3]) ? explode(',', $listData[3]) : [];
                    $list   = call_user_func_array([$class, $fun], $params);
                    break;
                case 'D': //执行指定模型方法 模型路径:方法名:参数1,参数2,参数3
                    $class  = $listData[1];
                    $fun    = $listData[2];
                    $params = isset($listData[3]) ? explode(',', $listData[3]) : [];
                    $list   = call_user_func_array([D($class), $fun], $params);
                    break;
                case 'static': //静态数据 1|男,2|女
                    $arr  = explode(',', $listData[1]);
                    $list = [];
                    foreach ($arr as $v) {
                        if(strpos($v,'|') !== false){
                            $item   = explode('|', $v);
                            $list[] = ['key' => $item[0], 'name' => $item[1]];
                        }else{
                            $list[] = ['key' => $v, 'name' => $v];
                        }
                    }
                    break;
            }
        } else {
            if (is_object($listData)) $listData = $listData->isEmpty() ? [] : $listData->toArray(); //处理对象转数组
            $list = $listData;
        }
        return $list;
    }
}